require(['jquery', 'cookie', 'app', 'lay'], function ($) {
    require(['vue'], function (Vue) {
        layui.use(['form','laydate','upload'], function() {
            var form = layui.form;
            form.on('submit(formDemo)', function(data){
                debugger;
                dic.that['image']=JSON.stringify(dic.that.image);
                dic.that['note']=JSON.stringify(dic.that.note);
                var shop = dic.that;
                shop['shopClass']['status']="ING";
                shop['shopClass']['band']="ING";
                if(shop['shopActivity']['uuid']!=''){
                    shop['shopActivity']['status']="ING";
                }else{
                    delete shop['shopActivity'];
                }

                shop['status']="";
                var spce = dic.specList;
                for(var i=0;i<spce.length;i++){
                    spce[i]['status']="";
                    spce[i]['shop']="";
                }
                if(dic.spec==0){
                    alr("请设置最少一种规格！");
                    return false;
                }
                shop['brand']=shop['brand'].trim();
                var re = playAjax("/ShopController/AddShop",{shop:shop,spec:spce});
                dic.that['image']=JSON.parse(dic.that.image);
                alr(re.msg);
                setTimeout(function () {
                    window.reload();
                },500);
                return false;
            });
            var upload=layui.upload;
            //执行实例
            var uploadInst = upload.render({
                elem: '#upload' //绑定元素
                , url: configPro.server_img_interface //上传接口
                , done: function (res) {
                    if(dic.that.image==null){
                        dic.that.image=new Array();
                        dic.that.image.push(res.data.src);
                    }else{
                        dic.that.image.push(res.data.src);
                    }
                }
                , error: function (res) {
                    alr("上传失败");
                }
                , accept: 'file'
            });
            var uploadInstX = upload.render({
                elem: '#uploadX' //绑定元素
                , url: configPro.server_img_interface //上传接口
                , done: function (res) {
                    if(dic.that.note==null){
                        dic.that.note=new Array();
                        dic.that.note.push(res.data.src);
                    }else{
                        dic.that.note.push(res.data.src);
                    }
                }
                , error: function (res) {
                    alr("上传失败");
                }
                , accept: 'file'
            });
        });
        var dic = new Vue({
            el: ".vue-box"
            , data: {
                where: [],
                that: {
                    isIndex:0,
                    uuid:"",
                    name:"",
                    icon:"",
                    spack:"",
                    makePrice:"",
                    unit:"",
                    brand:"",
                    place:"",
                    type:"",
                    image:[],
                    shopClass:{
                        uuid:"",
                        name:""
                    },
                    shopActivity:{
                        uuid:"",
                        name:""
                    },
                    note:[]
                },
                spec:0,
                specList:[
                ]
            }
            , created: function () {
                var uuid=new acquire().parameterMap['uuid'];
                if(uuid!=null){
                    var da=playAjax("ShopController/GetShop",{uuid:uuid}).data;
                    debugger;
                    this.that=da.shop;
                    this.that.image=JSON.parse(this.that.image);
                    this.that.note=JSON.parse(this.that.note);
                    if(this.that.shopActivity==null){
                        this.that.shopActivity={
                            uuid:"",
                            name:""
                        };
                    }
                    this.spec=da.spec.length;
                    this.specList=da.spec;
                }
                this.add();

            }
            , methods: {
                delNote:function (i) {
                    var re = this.that.image;
                    re.splice(i,1);
                    this.that.image=re;
                },
                delNoteX:function (i) {
                    var re = this.that.note;
                    re.splice(i,1);
                    this.that.note=re;
                },
                referXadmin: function () {
                    window.location.reload();
                },
                add:function () {
                    var sp = this.spec;
                    this.spec=sp+1;
                    var list = this.specList;
                    list.push({
                        name:"",
                        price:"",
                        cose:""
                    });
                    this.specList=list;
                },
                delG:function (i) {
                    var sp = this.spec;
                    this.spec=sp-1;
                    var list = this.specList;
                    list.splice(i,1);
                    this.specList=list;
                },
                fl:function (e) {
                    e = e.currentTarget;
                    debugger;
                    layer.open({
                        type: 2
                        , content: "./33"
                        , area: ["800px", "400px"]
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var body = layer.getChildFrame('body', index);
                            var w = $(layero).find("iframe")[0].contentWindow;
                            var jon = w.checkSure();
                            $(e).val(jon['name']);
                            $(e).attr("uuid", jon['uuid']);
                            dic.that.shopClass={uuid:jon['uuid'],name:jon['name']};
                            layer.closeAll();
                        }
                        , btn2: function (index, layero) {
                            layer.closeAll();
                        }
                        , cancel: function () {
                            layer.closeAll();
                        }
                    });
                },
                hd:function (e) {
                    e = e.currentTarget;
                    layer.open({
                        type: 2
                        , content: "./32"
                        , area: ["800px", "400px"]
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var body = layer.getChildFrame('body', index);
                            var w = $(layero).find("iframe")[0].contentWindow;
                            var jon = w.checkSure();
                            $(e).val(jon['name']);
                            $(e).attr("uuid", jon['uuid']);
                            dic.that.shopActivity={uuid:jon['uuid'],name:jon['name']};

                            layer.closeAll();

                        }
                        , btn2: function (index, layero) {
                            layer.closeAll();
                        }
                        , cancel: function () {
                            layer.closeAll();
                        }
                    });
                }
            }
        });
    })
});
